package hjj.servlet;

import hjj.service.DeptManager;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Dept;

import util.Page;

public class DeptManagerServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		int pageNo = 1;
		int pageSize = 10;
		DeptManager dm = new DeptManager();
		String deptqueryinfo = "";
		String deptquerytype = "";
		Page<Dept> page = null;
		try {
			if (request.getParameter("pageNo") != null) {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			}
			if (request.getParameter("pageSize") != null) {
				pageSize = Integer.parseInt(request.getParameter("pageSize"));

				request.getSession().setAttribute("pageSize", pageSize);
			}
			if (request.getSession().getAttribute("pageSize") != null) {
				pageSize = (Integer) request.getSession().getAttribute(
						"pageSize");
			}
			if (null != request.getParameter("deptquerytype")) {
				pageNo = 1;
				request.getSession().setAttribute("deptquerytype",
						request.getParameter("deptquerytype"));
			}
			if (null != request.getParameter("deptqueryinfo")) {
				request.getSession().setAttribute("deptqueryinfo",
						request.getParameter("deptqueryinfo"));
			}
			if (null != request.getSession().getAttribute("deptquerytype")) {
				deptquerytype = (String) request.getSession().getAttribute(
						"deptquerytype");
			}
			if (null != request.getSession().getAttribute("deptqueryinfo")) {
				deptqueryinfo = (String) request.getSession().getAttribute(
						"deptqueryinfo");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!"".equals(deptqueryinfo)&&!"deptall".equals(deptquerytype)) {
			if ("deptname".equals(deptquerytype)) {
				page = dm.getDeptByName(pageNo, pageSize, deptqueryinfo);
			}
			if ("deptheader".equals(deptquerytype)) {
				page = dm.getDeptByHeaderName(pageNo, pageSize, deptqueryinfo);
			}
			if ("deptstatus".equals(deptquerytype)) {
				page = dm.getDeptByStatus(pageNo, pageSize, deptqueryinfo);
			}

		} else {
			page = dm.getAllDept(pageNo, pageSize);
		}
		request.setAttribute("page", page);
		request.getRequestDispatcher("System/DeptManager.jsp").forward(request,
				response);
	}

}
